package com.jl.commons.base;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jl.commons.constant.Constants;
import com.jl.commons.constant.ErrorCode;
import com.jl.commons.constant.ErrorMessage;
import com.jl.commons.result.ResultData;
import com.jl.commons.shiro.ShiroUser;
import com.jl.model.User;
import com.jl.model.in.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


/**
 * @Desc Controller基类
 */
@Controller
public class BaseController {
	protected final Logger logger = LoggerFactory.getLogger(BaseController.class);
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	/**
	 * 获取当前登录用户对象
	 *
	 * @return {ShiroUser}
	 */
	public ShiroUser getShiroUser() {
		return (ShiroUser) SecurityUtils.getSubject().getSession().getAttribute(Constants.CURRENT_USER);
	}

	public Long getUserId(){
		return getShiroUser().getId();
	}

	public ResultData success(Object content){

		ResultData resultData = new ResultData();
		resultData.setContent(content);
		resultData.setSuccess(true);
		return resultData;
	}

	public ResultData success(){
		ResultData resultData = new ResultData();
		resultData.setContent(Constants.SUCCESS);
		resultData.setSuccess(true);
		return resultData;
	}

	public ResultData fail(Object content,Integer errorCode,String errorMessage){
		ResultData resultData = new ResultData();
		resultData.setContent(content);
		resultData.setSuccess(false);
		resultData.setErrorCode(errorCode);
		resultData.setErrorMsg(errorMessage);
		return resultData;
	}

	public ResultData fail(Integer errorCode,String errorMessage){
		ResultData resultData = new ResultData();
		resultData.setSuccess(false);
		resultData.setErrorCode(errorCode);
		resultData.setErrorMsg(errorMessage);
		return resultData;
	}

	public ResultData fail(){
		ResultData resultData = new ResultData();
		resultData.setSuccess(false);
		resultData.setErrorCode(ErrorCode.ERROR);
		resultData.setErrorMsg(ErrorMessage.ERROR);
		return resultData;
	}

	public ResultData editFail(){
		ResultData resultData = new ResultData();
		resultData.setSuccess(false);
		resultData.setErrorCode(ErrorCode.EDIT_ERROR);
		resultData.setErrorMsg(ErrorMessage.EDIT_ERROR);
		return resultData;
	}

	public ResultData addFail(){
		ResultData resultData = new ResultData();
		resultData.setSuccess(false);
		resultData.setErrorCode(ErrorCode.ADD_ERROR);
		resultData.setErrorMsg(ErrorMessage.ADD_ERROR);
		return resultData;
	}
	/**
	 * @Desc list转 Map
	 * @param page
	 * @return
	 */
	public Map<String, Object> toLayuiList(final Page<?> page) {
		HashMap<String, Object> map = new HashMap<String, Object>(8);
		map.put("code", 0);
		map.put("msg", "success");
		map.put("page", page.getCurrent());
		map.put("count", page.getTotal());
		map.put("data", page.getRecords());
		return map;
	}
}
